package com.haoze.nurse.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haoze.common.enumeration.StateEnum;
import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.nurse.dto.Assembly;
import com.haoze.nurse.dto.NurseBookDTO;
import com.haoze.nurse.dto.RoleTypeEntity;
import com.haoze.nurse.dto.WardSummaryDTO;
import com.haoze.nurse.enumeration.MessageEnum;
import com.haoze.nurse.model.MessageEntity;
import com.haoze.nurse.service.NurseFileService;
import com.haoze.nurse.service.NurseOrderService;
import com.haoze.nurse.service.NurseStatisticsService;
import com.haoze.nurse.vo.NurseFile;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.ws.rs.GET;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/nurse/file")
@Validated
@Api(value = "护理记录")
public class NurseFileController {

    @Resource
    NurseFileService nurseFileService;
    @Resource
    NurseStatisticsService nurseStatisticsService;
    @Resource
    NurseOrderService nurseOrderService;

    /**
     * 护理信息修改痕迹
     * @param page 页码
     * @param pageSize 长度
     * @param inpatNum 住院号
     * @return
     *
     */
    @GetMapping("/modifyRecord")
    @ApiOperation(value = "获取修改痕迹列表数据")
    public Result modifyRecord(@RequestParam(defaultValue = "1") final Integer page,@RequestParam(defaultValue = "20") final Integer pageSize,
                               @RequestParam final String inpatNum,@RequestParam final String recordType,
                               @RequestParam(defaultValue = "",required = false) final String createTime,
                               @RequestParam(defaultValue = "",required = false) final String recordTime,
                               @RequestParam(defaultValue = "",required = false) final String userName,//本次记录修改人
                               @RequestParam(defaultValue = "",required = false) final String createUserName,//当前记录创建人
                               @RequestParam(defaultValue = "",required = false) final String modifyLocation,
                               @RequestParam(defaultValue = "",required = false) final String modifyType){
        return nurseStatisticsService.modifyRecord(page, pageSize, inpatNum, recordType, createTime, recordTime, userName,createUserName,modifyLocation,modifyType);
    }


    @GetMapping("/getModifyLocation")
    @ApiOperation(value = "获取修改位置（模板中节点名称）")
    public Result getModifyLocation(@RequestParam String recordType,@RequestParam String inpatNum) {
        return nurseFileService.getModifyLocation(recordType,inpatNum);
    }



    /**
     * 获取列表数据
     * @param page 页码
     * @param pageSize 长度
     * @param inpatNum 住院号
     * @param recordType 记录单类型
     * @return
     */
    @GetMapping("/fileList")
    @ApiOperation(value = "获取列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "String", required = true, defaultValue = "1", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页长", dataType = "String", required = true, defaultValue = "20", example = "20"),
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", required = true, defaultValue = "1234567-1", example = "1234567-1"),
            @ApiImplicitParam(name = "recordType", value = "记录类型", dataType = "String", required = true, defaultValue = "1", example = "1"),
            @ApiImplicitParam(name = "beginTime", value = "起始时间", dataType = "String", required = false, defaultValue = "2020-01-01 10:10", example = "2020-01-01 10:10"),
            @ApiImplicitParam(name = "endTime", value = "截止时间", dataType = "String", required = false, defaultValue = "2020-02-01 10:10", example = "2020-02-01 10:10"),
            @ApiImplicitParam(name = "orderType", value = "排序类型(1正序,2倒序)", dataType = "String", required = false, defaultValue = "asc", example = "asc"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = NurseBookDTO.class)
    })
    public Result fileList (@RequestParam(defaultValue = "1") final String page,
                            @RequestParam(defaultValue = "20") final String pageSize,
                            @RequestParam(defaultValue = "",required = false) final String inpatNum,
                            @RequestParam(defaultValue = "",required = false) final String recordType,
                            @RequestParam(defaultValue = "",required = false) final String beginTime,
                            @RequestParam(defaultValue = "",required = false) final String endTime,
                            @RequestParam(defaultValue = "1",required = false)  String orderType
                            ) {


        return nurseFileService.getFileList(page, pageSize, inpatNum, recordType, beginTime, endTime,orderType);
    }

    /**
     * 获取列表数据
     * @param page 页码
     * @param pageSize 长度
     * @param inpatNum 住院号
     * @param recordType 记录单类型
     * @return
     */
    @GetMapping("/printFileList")
    @ApiOperation(value = "获取打印列表数据")
    public Result printFileList (@RequestParam(defaultValue = "1") final String page,
                                 @RequestParam(defaultValue = "20") final String pageSize,
                                 @RequestParam(defaultValue = "",required = false) final String inpatNum,
                                 @RequestParam(defaultValue = "",required = false) final String recordType,
                                 @RequestParam(defaultValue = "",required = false) final String beginTime,
                                 @RequestParam(defaultValue = "",required = false) final String endTime) {
        return nurseFileService.getPrintFileList(page, pageSize, inpatNum, recordType,beginTime,endTime);
    }

    @GetMapping("/printFileSum")
    @ApiOperation(value = "打印护理记录总数")
    public Result getPrintFileSum(@RequestParam final String inpatNum,
                                  @RequestParam final String recordType,
                                  @RequestParam(defaultValue = "",required = false) final String beginTime,
                                  @RequestParam(defaultValue = "",required = false) final String endTime) {
        return nurseFileService.getPrintFileSum(inpatNum, recordType,beginTime,endTime);
    }

    @GetMapping("/create")
    @ApiOperation(value = "新增护理记录")
    public Result create(@RequestParam final String inpatNum,
                         @RequestParam final String recordType) {
        return nurseFileService.createNewRecord(inpatNum, recordType);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存护理记录", notes = "0")
    public Result saveNurseFile(@RequestBody final String json) {
        NurseFile nurseFile = JSONObject.parseObject(json, NurseFile.class);
        return nurseFileService.save(nurseFile);
    }

    @PostMapping("/editSave")
    @ApiOperation(value = "编辑保存护理记录", notes = "0")
    public Result editSaveNurseFile(@RequestBody final String json) {
        NurseFile nurseFile = JSONObject.parseObject(json, NurseFile.class);
        return nurseFileService.editSave(nurseFile);
    }

    @GetMapping("/oneRecord")
    @ApiOperation(value = "获取一条记录")
    public Result getOneRecord(@RequestParam final String fileDataId,
                               @RequestParam final String inpatNum,
                               @RequestParam final String recordType) {
        return nurseFileService.getOneRecord(fileDataId, inpatNum, recordType);
    }

    @GetMapping("/queryRegisterBirthData")
    public Result queryRegisterBirthData(@RequestParam final String inpatNum,
                                         @RequestParam final String recordType) {
        return nurseFileService.queryRegisterBirthData(inpatNum, recordType);
    }

    @PostMapping("/deleteRecord")
    @ApiOperation(value = "删除一条记录")
    public Result deleteOneRecord(@RequestBody final String json) {
        Map<String, String> map = JSONObject.parseObject(json, Map.class);
        return nurseFileService.deleteOneRecord(map);
    }

    @PostMapping("/bondVarcode")
    @ApiOperation(value = "绑定varCode")
    public Result bondVarCode(@RequestBody final String json) {
        Map<String, String> map = JSONObject.parseObject(json, Map.class);
        return nurseFileService.bondVarcode(map);
    }

    @GetMapping("/apgarScore")
    @ApiOperation(value = "获取apgar评分详情")
    public Result getApgarScore(@RequestParam final String inpatNum) {
        return nurseFileService.getApgarScore(inpatNum);
    }

    @PostMapping("/isSummary")
    @ApiOperation(value = "是否有出入量总结")
    public Result isSummary(@RequestBody final Map<String, String> map) {
        return nurseFileService.isSummary(map);
    }

    @PostMapping("/inoutSummary")
    @ApiOperation(value = "出入量总结")
    public Result inoutSummary(@RequestBody final String json) {
        Map<String, String> map = JSONObject.parseObject(json, Map.class);
        return nurseFileService.createInputOutputSummary(map);
    }

    @PostMapping("/deleteSummary")
    @ApiOperation(value = "删除出入量总结记录")
    public Result deleteSummary(@RequestBody final String json) {
        Map<String, String> map = JSONObject.parseObject(json, Map.class);
        return nurseFileService.deleteSummaryRecord(map);
    }

    @GetMapping("/sign")
    @ApiOperation(value = "签名")
    public Result sign(@RequestParam final String loginName,
                       @RequestParam final String userPwd,
                       @RequestParam(required = false) final String fileDataId) {
        return nurseFileService.sign(loginName, userPwd,fileDataId);
    }

    @GetMapping("/tplDetail")
    @ApiOperation(value = "获取模板详情")
    public Result getTplDetail(@RequestParam final String tplDetailId,
                               @RequestParam(required = false) String inpatNum,
                               @RequestParam(required = false) String recordType) {
        return nurseFileService.getTplDetail(tplDetailId, inpatNum, recordType);
    }

    @GetMapping("/inOutType")
    @ApiOperation(value = "获取出入量类型")
    public Result inOutValueType(@RequestParam final String inpatNum,
                                 @RequestParam final String recordTime) {
        return nurseStatisticsService.getInOutValueType(inpatNum, recordTime);
    }

    @GetMapping("/getPreBedNo")
    @ApiOperation(value = "根据时间获取病人实时床号")
    @ApiImplicitParams({@ApiImplicitParam(name = "inpatNum", value = "入院号", dataType = "String", required = true, defaultValue = "", example = "1077179-1"),
                        @ApiImplicitParam(name = "recordTime", value = "时间", dataType = "String", required = true, defaultValue = "", example = "2020-04-03 09:30")})
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = String.class)
    })
    public Result getPreBedNo(@RequestParam final String inpatNum,
                              @RequestParam final String recordTime){
        return nurseStatisticsService.getPreBedNo(inpatNum,recordTime);
    }

    @GetMapping("/doStatistics")
    @ApiOperation(value = "计算出入量统计")
    public Result doStatistics(@RequestParam final String type,
                               @RequestParam final String recordTime,
                               @RequestParam final String inpatNum) {
        return nurseStatisticsService.doStatistics(type, recordTime, inpatNum);
    }

    @GetMapping("/rule")
    @ApiOperation(value = "获取规则")
    public Result getRule(@RequestParam final String inpatNum,
                          @RequestParam final String recordTime,
                          @RequestParam final String recordType,
                          @RequestParam final String varCode) {
        return nurseFileService.getRule(inpatNum, recordTime, recordType, varCode);
    }

    @GetMapping("/getFileIndexId")
    @ApiOperation(value = "获取护理文书Id")
    public Result getFileIndexId(@RequestParam final String inpatNum,
                                 @RequestParam final String recordType){
        return nurseFileService.getFileIndexId(inpatNum, recordType);
    }

    @GetMapping("/allRemarks")
    @ApiOperation(value = "获取当前用户全量备注")
    public Result getAllRemarks(@RequestParam final String fileIndexId,
                                @RequestParam(defaultValue ="0") final Integer type) {
        return nurseFileService.getAllRemarks(fileIndexId, type);
    }
    @GetMapping("/otherPipes")
    @ApiOperation(value = "获取其他管道")
    public Result getOtherPipes(@RequestParam final String type,
                                @RequestParam final String fileIndexId) {
        return nurseFileService.getOtherPipes(type, fileIndexId);
    }

    @GetMapping("/saveRemark")
    @ApiOperation(value = "保存扩展备注")
    public Result saveRemark(@RequestParam final String type,
                             @RequestParam final String fileIndexId,
                             @RequestParam final String name) {
        return nurseFileService.saveRemark(type, fileIndexId, name);
    }
    @GetMapping("/deleteRemark")
    @ApiOperation(value = "删除备注")
    public Result deleteRemark(@RequestParam final String fileIndexId,
                               @RequestParam final String type,
                               @RequestParam final String name) {
        return nurseFileService.deleteRemark(fileIndexId, type, name);
    }

    @GetMapping("/queryselection")
    @ApiOperation(value = "更多选项")
    public Result querySelections(@RequestParam (required = false) final String elementId,
                                  @RequestParam(required = false) String dataCode,
                                  @RequestParam(defaultValue = "", required = false) final String word,
                                  @RequestParam(defaultValue = "1") final int page,
                                  @RequestParam(defaultValue = "20") final int pageSize,
                                  @RequestParam(required = false) final String parentSerial,
                                  @RequestParam(required = false) String varCode,
                                  @RequestParam(required = false) String wardId,
                                  @RequestParam(defaultValue = "",required = false) String isWeight) {//搜索时是否按照权重排序
        List<Map<String, String>> selections = nurseFileService.querySelections(elementId, word, dataCode, page, pageSize, parentSerial, varCode, wardId,isWeight);
        return ResultGenerator.genOkResult(selections);
    }

    @GetMapping("/transData")
    @ApiOperation(value = "转换数据")
    public Result transData(@RequestParam (required = false) final String inpatNum) {
        return nurseFileService.transData(inpatNum);
    }

    @GetMapping("/searchName") // 此接口后续可能扩展蛋白类型，目前只获取血制品
    @ApiOperation(value = "根据搜索获取血制品")
    public Result searchBlood(@RequestParam final String searchName) {
        return nurseFileService.searchName(searchName);
    }


    @PostMapping("/docAdvice")
    @ApiOperation(value = "获取选中医嘱（节点返回）")
    public Result doctorAdvice(@RequestBody final String json) {
        List list = nurseOrderService.getDoctorAdviceNodes(json);
        return ResultGenerator.genOkResult(list);
    }

    @GetMapping("/getApgarRecord")
    @ApiOperation(value = "获取Apgar评分记录(生成医疗术语)")
    public Result getApgarRecord(@RequestParam final String inpatNum) {
        return nurseFileService.getApgarRecord(inpatNum);
    }

    @GetMapping("/words")
    @ApiOperation(value = "通过关键字检索值域、医疗术语、数据元")
    public Result associateByWords(@RequestParam final String word,
                                   @RequestParam(required = false) final String varCode) {
        List<Assembly> wordList = nurseFileService.associateByWords(word, varCode);
        return ResultGenerator.genOkResult(wordList);
    }

    @GetMapping("/getUnits")
    @ApiOperation(value = "获取数字型数据元单位下拉项")
    public Result getUnits(@RequestParam(required = true) final String elementId) {
        List<String> list = nurseFileService.getUnits(elementId);
        return ResultGenerator.genOkResult(list);
    }

    @GetMapping("/getCatalog")
    @ApiOperation(value = "获取对应目录")
    public Result getCatalog(@RequestParam(required = true) final String deptId,@RequestParam(required = true) final String inpatNum) {
        return nurseFileService.getCatalog(deptId,inpatNum);
    }

    @GetMapping("/currSystime")
    @ApiOperation(value = "获取当前系统时间")
    public Result getCurrSystime() {
        return nurseFileService.getCurrSystime();
    }


    @GetMapping("/dynamicCascade")
    @ApiOperation(value = "级联")
    public Result dynamicCascade(@RequestParam String medicalId,//医疗术语ID
                                 @RequestParam(required = false) final String dataRelationIds,//值域id或者数据元id
                                 @RequestParam(required = false) final String numberValue//对应数值
    ) {
      return ResultGenerator.genOkResult(nurseFileService.dynamicCascade(medicalId, dataRelationIds, numberValue));
    }

    @GetMapping("/remark") // 此接口后续可能扩展蛋白类型，目前只获取血制品
    @ApiOperation(value = "获取备注")
    public Result getRemark(@RequestParam final String inpatNum,
                            @RequestParam final String recordType) {
        return nurseFileService.getRemark(recordType, inpatNum);
    }

    @GetMapping("/delRemark")
    @ApiOperation(value = "删除备注")
    public Result getRemark(@RequestParam final String tplDetailId,
                            @RequestParam final String inpatNum,
                            @RequestParam(required = false) final String recordType,
                            @RequestParam final String name) {
        return nurseFileService.delRemark(tplDetailId, recordType, inpatNum, name);
    }

    @GetMapping("/getAssess")
    @ApiOperation(value = "获取评估项目下拉项")
    public Result getAssess(@RequestParam final String varCode) {
        return nurseFileService.getAssess(varCode);
    }


    @GetMapping("/ownTemplate")
    @ApiOperation(value = "获取对应模板是否有下级节点")
    public Result ownTemplate(@RequestParam final String basicValueId) {
        return nurseFileService.ownTemplate(basicValueId);
    }

    @PostMapping("/medicalTree")
    @ApiOperation(value = "医疗术语树")
    public Result medicalTree(@RequestBody RoleTypeEntity roleTypeEntity) {
        return nurseFileService.medicalTree(roleTypeEntity);
    }


    @GetMapping("/getFormListByUser")
    @ApiOperation(value = "获取所有表单")
    public Result getFormListByUser() {
        return nurseFileService.getFormListByUser();
    }


    @PostMapping("/wardDeptSummary")
    @ApiOperation(value = "病区病人汇总")
    public Result wardDeptSummary(@RequestBody WardSummaryDTO dto) {
        return nurseFileService.wardDeptSummary(dto);
    }

    @PostMapping("/wardDeptSummaryDetail")
    @ApiOperation(value = "病区病人汇总详情")
    public Result wardDeptSummaryDetail(@RequestBody WardSummaryDTO dto) {
        return nurseFileService.wardDeptSummaryDetail(dto);
    }

    @GetMapping("/messageList")
    public Result list(@RequestParam Map<String, Object> params){
        return nurseFileService.list(params);
    }

    @GetMapping("/careUnit")
    @ApiOperation(value = "获取护理单位")
    public Result getCareUnit() {
        return nurseFileService.getCareUnit();
    }

}
